package cn.wolfcode.plus.util;

import cn.wolfcode.plus.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String EMP_IN_SESSION = "EMP_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    /**
     * 利用springmvc提供的工具类获取session对象
     * @return
     */
    public static HttpSession getSession() {
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    /**
     * 从session中获取登录用户
     * @return
     */
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(EMP_IN_SESSION);
    }

    /**
     * 把用户存到session中
     * @return
     */
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMP_IN_SESSION,employee);
    }

    /**
     * 把权限表达式集合存到session中
     * @param list
     */
    public static void setExpressions(List<String> list) {
        getSession().setAttribute(EXPRESSION_IN_SESSION,list);
    }

    /**
     * 从session中获取权限表达式集合
     * @return
     */
    public static List<String> getExpressions() {
        return (List<String>)getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
